package com.hyperzsoft.budget;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.Toast;

public class MainView extends Activity {
	
	static private String TAG = "MainView";
	final CalendarDialog cd = new CalendarDialog();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.main);
		setContentView(R.layout.budget_grid);
	}
	
	private void setupBudgetGrid() {
		TableLayout tl = (TableLayout) findViewById(R.id.budget_table);
		TableRow tr = (TableRow) findViewById(R.id.tableRow1);
		LayoutParams trp = new TableRow.LayoutParams();
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		try {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.main_menu, menu);
			return true;
		} catch (Exception e) {
			Log.e(TAG, e.getMessage().toString());
			for (StackTraceElement traceElement : e.getStackTrace()) {
				Log.e(TAG, "  at " + traceElement.toString());
			}
			return false;
		}
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent myIntent = new Intent();
		
		try {
			// Handle item selection
			switch (item.getItemId()) {
			case R.id.expenses_list:
				myIntent.setClassName("com.hyperzsoft.budget", "com.hyperzsoft.budget.EditExpenses");
				startActivity(myIntent);
				
				return true;
			case R.id.edit_types:
				myIntent.putExtra("type_of", new DBProvider(getApplicationContext()).EXPENSE_TYPES);
				myIntent.setClassName("com.hyperzsoft.budget", "com.hyperzsoft.budget.EditTypes");
				startActivity(myIntent);
				
				return true;
			case R.id.create_db:
				new CreateData().createDefaultGroupsAndItems(getApplicationContext());
				
				return true;
				//14886
			case R.id.extra:
				createCalendarDialog();
				cd.setCalendarValue(1286150400000L);
				return true;
			case R.id.quit:
				finish();
				
				return true;
			default:
				return super.onOptionsItemSelected(item);
			}
		} catch (Exception e) {
			Log.e(TAG, e.getMessage().toString());
			for (StackTraceElement traceElement : e.getStackTrace()) {
				Log.e(TAG, "  at " + traceElement.toString());
			}
			return false;
		}
	}
	
	private void createCalendarDialog() {
		cd.create(this);
		cd.setDismissListener(cd.new customDismissListener() {
			@Override
			public void onDismiss(DialogInterface dialogInterface) {
				Toast.makeText(getApplicationContext(), cd.getCalendarValue(), Toast.LENGTH_LONG).show();
				dialogInterface.dismiss();
			}
		});
	}
	
	@Override
	protected void onSaveInstanceState (Bundle outState) {
		if (cd.isShowing()) {
			outState.putLong("cal_in_millis", cd.getCalendarValueInMillis());
		}
	}
	
	@Override
	protected void onRestoreInstanceState (Bundle savedInstanceState) {
		if (savedInstanceState.getLong("cal_in_millis", 0) != 0) {
			createCalendarDialog();
			cd.setCalendarValue((savedInstanceState.getLong("cal_in_millis")));
		}
	}
}
